package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceSaveParam;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Autowired
    VehicleMapper vehicleMapper;
    @Override
    public List<GeofenceVO> loadGeofence(GeofenceQuery geofenceQuery) {
        log.debug("加载围栏业务，参数：{}",geofenceQuery);
        List<GeofenceVO> list = geofenceMapper.loadGeofence(geofenceQuery);
        //处理围栏车辆数据
        //遍历每个围栏，获取围栏车辆数据
        for (int i = 0;i<list.size();i++){
            GeofenceVO geofenceVO = list.get(i);
            Integer totalNum = 0;
            Integer availableNum = 0;
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
            totalNum = vehicleVOList.size();
            if (vehicleVOList != null && vehicleVOList.size()>0){
                for (int j = 0;j<vehicleVOList.size();j++){
                    VehicleVO vehicleVO = vehicleVOList.get(j);
                    if (vehicleVO.getStatus().equals("1")){
                        availableNum++;
                    }
                }
            }
            geofenceVO.setTotalNum(totalNum);
            geofenceVO.setAvailableNum(availableNum);
            geofenceVO.setVehicleList(vehicleVOList);
        }
        return list;
    }

    @Override
    public void saveGeofence(GeofenceSaveParam geofenceSaveParam) {
        log.debug("新增围栏业务，参数:{}",geofenceSaveParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceSaveParam,geofence);
        if (geofence.getId() == null){
            //每个围栏初始时都是启用状态
            geofence.setStatus("1");
            geofence.setCreateTime(new Date());
            geofenceMapper.insert(geofence);
        }else {
            geofence.setUpdateTime(new Date());
            geofenceMapper.updateGeofence(geofence);
        }
    }

    @Override
    public void deleteGeofence(Long geofenceId) {
        log.debug("删除围栏业务，参数：{}",geofenceId);
        //业务：需要判断当前围栏上是否有已绑定的车辆，如果有绑定车辆就不能删除
        //1.封装车辆查询的DTO层对象VehicleQuery
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(geofenceId);
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        if (list.size()>0 && list != null){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }else {
            geofenceMapper.deleteGeofence(geofenceId);
        }
    }
}
